<template>
	<view>
		<uni-nav-bar dark :fixed="true" shadow background-color="red" status-bar left-icon="left" left-text="返回"
			title="地址管理" @clickLeft="goBack"></uni-nav-bar>

		<view class="container">
			<view class="address-list">
				<view class="address-item" v-for="(item, index) in addressList" :key="index">
					<view class="address-info">
						<view class="tag-row">
							<text class="tag">{{ item.description }}</text>
							<!-- <text class="default-tag" v-if="index === 0">默认</text> -->
						</view>
						<view class="detail">{{ item.address }}</view>
						<view class="coordinates">
							<text>经度: {{ item.longitude }}</text>
							<text>纬度: {{ item.latitude }}</text>
						</view>
					</view>
					<view class="address-actions">
						<view class="delete" @tap="deleteAddress(item.id)">删除</view>
					</view>
				</view>
			</view>

			<view class="empty" v-if="addressList.length === 0">
				<image src="/static/img/empty-address.png" mode="aspectFit" class="empty-image"></image>
				<text class="empty-text">暂无地址，请添加</text>
			</view>

			<view class="add-btn" @tap="goToAdd">
				<text class="add-icon">+</text>
				<text>新增地址</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		GetPromotionUserAddressList,
		PromotionUserAddressDel
	} from "@/api/promotion";
	export default {
		data() {
			return {
				addressList: []
			}
		},

		onShow() {
			this.getAddressList()
		},

		methods: {
			goBack() {
				uni.navigateBack()
			},

			getAddressList() {
				var form = {
					"OpenId": this.$store.state.user.openid
				};
				// 从本地存储获取地址列表
				GetPromotionUserAddressList(form)
					.then(res => {
						if (res.status == 0) {
							this.addressList = res.data;
						}

					})
					.catch(res => {});
			},

			goToAdd() {
				uni.navigateTo({
					url: '/pages/address/add'
				})
			},


			deleteAddress(id) {
				uni.showModal({
					title: '提示',
					content: '确定要删除该地址吗？',
					success: (res) => {
						if (res.confirm) {
							PromotionUserAddressDel({
									id
								})
								.then(res => {
									if (res.status == 0) {
										uni.showToast({
											title: '删除成功',
											icon: 'success'
										})
									}

								})
								.catch(res => {});
							let list = this.addressList.filter(item => item.id !== id)
							this.addressList = list

						}
					
					}
				})
			
			}
		}
	}
</script>

<style>
	.container {
		padding: 20rpx;
		min-height: 100vh;
		background: #f5f5f5;
	}

	.address-list {
		margin-top: 20rpx;
	}

	.address-item {
		background: #fff;
		padding: 30rpx;
		margin-bottom: 20rpx;
		border-radius: 16rpx;
		display: flex;
		justify-content: space-between;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
	}

	.address-info {
		flex: 1;
	}

	.tag-row {
		display: flex;
		align-items: center;
		margin-bottom: 16rpx;
	}

	.tag {
		font-size: 32rpx;
		color: #333;
		font-weight: bold;
	}

	.default-tag {
		font-size: 24rpx;
		color: #FC0000;
		border: 1px solid #FC0000;
		padding: 2rpx 12rpx;
		border-radius: 20rpx;
		margin-left: 20rpx;
	}

	.detail {
		font-size: 28rpx;
		color: #666;
		margin-bottom: 16rpx;
		line-height: 1.4;
	}

	.coordinates {
		font-size: 24rpx;
		color: #999;
	}

	.coordinates text {
		margin-right: 30rpx;
	}

	.address-actions {
		display: flex;
		align-items: center;
	}

	.delete {
		padding: 12rpx 24rpx;
		font-size: 28rpx;
		color: #FC0000;
		border: 1px solid #FC0000;
		border-radius: 30rpx;
	}

	.empty {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding-top: 200rpx;
	}

	.empty-image {
		width: 240rpx;
		height: 240rpx;
		margin-bottom: 30rpx;
	}

	.empty-text {
		font-size: 28rpx;
		color: #999;
	}

	.add-btn {
		position: fixed;
		bottom: 40rpx;
		left: 50%;
		transform: translateX(-50%);
		background: linear-gradient(to right, #FF4B4B, #FC0000);
		color: #fff;
		padding: 24rpx 100rpx;
		border-radius: 45rpx;
		font-size: 32rpx;
		display: flex;
		align-items: center;
		box-shadow: 0 4rpx 16rpx rgba(252, 0, 0, 0.3);
	}

	.add-icon {
		font-size: 40rpx;
		margin-right: 10rpx;
	}
</style>